/*----------------------------------------------------------------------------
 * Main Visual
 * ---------------------------------------------------------------------------
 * Date      : 2011-02-08
 * Author    : naiyumie
 * ---------------------------------------------------------------------------
 * USAGE
 * ---------------------------------------------------------------------------
 *
 * ---------------------------------------------------------------------------
 * EXAMPLE
 * ---------------------------------------------------------------------------
 *
 *----------------------------------------------------------------------------
 */
$.fn.MainVisual	= function (option)
{
	option = option || {};

	var element		= $(this);
	var navi		= $(this).children('div#visual-navi');
	var wrap		= $(this).children('div#visual-warp');

	var interval	= null;
	var current		= 0;
	var delay		= option.delay ? option.delay*1000 : 4000;
	var fade		= option.fade ? option.fade*1000 : 800;
	var size		= parseInt($(wrap).children('div').size());
	var is_focus	= false;

	var ie6	= ($.browser.msie && parseInt($.browser.version, 10)==6) ? true : false;

	$(wrap).css({'overflow':'hidden', 'position':'relative'}).children().each(function(i)
	{
		if (i>0) { $(this).hide(); }
		if (ie6===false)
		{
			$(this).css({'position':'absolute', 'top':0, 'left':0, 'width':'100%'});
		}
	});

	// btn play
	$(navi).children('ul.player').children('li.btn-play').children('a:first').click(function()
	{
		if (is_focus===true)
		{
			rotate();
			interval	= setInterval(rotate, delay);
			is_focus	= false;
		}
		return false;
	});

	// btn stop
	$(navi).children('ul.player').children('li.btn-stop').children('a:first').click(function()
	{
		is_focus	= true;
		clearInterval(interval);
		return false;
	});

	$(navi).children('ul.navi').children('li').each(function(num)
	{
		$(this).children('a:first').click(function()
		{
			var index	= num-1;
			current	= index<0 ? (size-1) : index;

			clearInterval(interval);
			rotate();

			if (is_focus===false)
			{
				interval	= setInterval(rotate, delay);
			}

			return false;
		});
	});

	var rotate	= function ()
	{
		current++;

		if (current>=size) { current = 0; }

		var visible	= $(wrap).children('div:visible').index();
		var target	= $(wrap).children('div:nth-child('+(visible+1)+')');

		if (ie6===true)
		{
			$(target).hide();
			$(wrap).children('div:nth-child('+(current+1)+')').show();
		}
		else
		{
			if (visible!=current)
			{
				$(target).css({'z-index':200}).fadeOut(fade, function(){});
			}

			$(wrap).children('div:nth-child('+(current+1)+')').css({'z-index':100}).show();
		}

		$(navi).children('ul.navi').find('a.active').removeClass('active');
		$(navi).children('ul.navi').children('li:nth-child('+(current+1)+')').children('a:first').addClass('active');
	};

	interval	= setInterval(rotate, delay);
};